﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;

namespace DBService
{
	public class XmlUtils
	{
		public static string GetPathName(Document doc)
		{
			string pathName = doc.PathName;
			int length = pathName.LastIndexOf("\\");
			return pathName.Substring(0, length);
		}

		public static void ClearDataFromConfigFile(string fileName, string noodName, string attributeName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("连接配置"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == noodName)
					{
						xmlNode.Attributes[attributeName].Value = "";
						break;
					}
				}
			}
			catch
			{
			}
			xmlDocument.Save(fileName);
		}

		public static string GetDataFromConfigFile(string fileName, string noodName, string attributeName)
		{
			string result = "";
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("连接配置"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == noodName)
					{
						result = xmlNode.Attributes[attributeName].Value;
						break;
					}
				}
			}
			catch
			{
			}
			return result;
		}

		public static void CreateDataFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
			xmlDocument.AppendChild(newChild);
			XmlElement xmlElement = xmlDocument.CreateElement("", "协同数据", "");
			xmlDocument.AppendChild(xmlElement);
			XmlElement newChild2 = xmlDocument.CreateElement("墙洞");
			XmlElement newChild3 = xmlDocument.CreateElement("板洞");
			XmlElement newChild4 = xmlDocument.CreateElement("房间信息");
			xmlElement.AppendChild(newChild2);
			xmlElement.AppendChild(newChild3);
			xmlElement.AppendChild(newChild4);
			xmlDocument.AppendChild(xmlElement);
			xmlDocument.Save(fileName);
		}

		public static string GetDataFileName(Document doc)
		{
			FileInfo fileInfo = new FileInfo(doc.PathName);
			int num = fileInfo.FullName.LastIndexOf(".");
			int num2 = fileInfo.FullName.LastIndexOf("\\");
			return fileInfo.FullName.Substring(num2 + 1, num - num2 - 1) + "_Data.xml";
		}

		private static string getGuid(string xmlValue)
		{
			string result;
			try
			{
				int num = xmlValue.IndexOf("-");
				result = xmlValue.Substring(num + 1, xmlValue.Length - num - 1);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = "";
			}
			return result;
		}

		public static bool AddRoonInfo(string fileName, DbRoomInfo info)
		{
			bool result;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "房间信息")
					{
						XmlElement xmlElement = xmlDocument.CreateElement("Id-" + info.Guid.ToString());
						xmlElement.SetAttribute("Name", info.Name);
						xmlElement.SetAttribute("Height", info.Height.ToString());
						xmlElement.SetAttribute("Number", info.Number);
						xmlElement.SetAttribute("Function", info.Function);
						xmlElement.SetAttribute("Material", info.Material);
						xmlElement.SetAttribute("RoomId", info.RoomId.ToString());
						xmlNode.AppendChild(xmlElement);
						break;
					}
				}
				xmlDocument.Save(fileName);
				result = true;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = false;
			}
			return result;
		}

		public static void RemoveRoomInfo(Guid id, string fileName)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "房间信息")
					{
						IEnumerator enumerator2 = xmlNode.ChildNodes.GetEnumerator();
						{
							while (enumerator2.MoveNext())
							{
								object obj2 = enumerator2.Current;
								XmlNode xmlNode2 = (XmlNode)obj2;
								if (xmlNode2.Name == "Id-" + id.ToString())
								{
									xmlNode.RemoveChild(xmlNode2);
									break;
								}
							}
							break;
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static List<DbRoomInfo> ReadRoomInfo(string fileName)
		{
			List<DbRoomInfo> list = new List<DbRoomInfo>();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "房间信息")
					{
						IEnumerator enumerator2 = xmlNode.ChildNodes.GetEnumerator();
						{
							while (enumerator2.MoveNext())
							{
								object obj2 = enumerator2.Current;
								XmlNode xmlNode2 = (XmlNode)obj2;
								string name = Convert.ToString(xmlNode2.Attributes["Name"].Value);
								double height = Convert.ToDouble(xmlNode2.Attributes["Height"].Value);
								string number = Convert.ToString(xmlNode2.Attributes["Number"].Value);
								int roomId = Convert.ToInt32(xmlNode2.Attributes["RoomId"].Value);
								string material = Convert.ToString(xmlNode2.Attributes["Material"].Value);
								string function = Convert.ToString(xmlNode2.Attributes["Function"].Value);
								list.Add(new DbRoomInfo(0, roomId, name, number, function, material, height)
								{
									Guid = Guid.Parse(XmlUtils.getGuid(xmlNode2.Name))
								});
							}
							break;
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return list;
		}

		public static DbRoomInfo ReadRoomInfo(Guid id, string fileName)
		{
			DbRoomInfo dbRoomInfo = null;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "房间信息")
					{
						IEnumerator enumerator2 = xmlNode.ChildNodes.GetEnumerator();
						{
							while (enumerator2.MoveNext())
							{
								object obj2 = enumerator2.Current;
								XmlNode xmlNode2 = (XmlNode)obj2;
								if (xmlNode2.Name == "Id-" + id.ToString())
								{
									string name = Convert.ToString(xmlNode2.Attributes["Name"].Value);
									double height = Convert.ToDouble(xmlNode2.Attributes["Height"].Value);
									string number = Convert.ToString(xmlNode2.Attributes["Number"].Value);
									Convert.ToInt32(xmlNode2.Attributes["RoomId"].Value);
									string material = Convert.ToString(xmlNode2.Attributes["Material"].Value);
									string function = Convert.ToString(xmlNode2.Attributes["Function"].Value);
									dbRoomInfo = new DbRoomInfo(0, 0, name, number, function, material, height);
									dbRoomInfo.Guid = Guid.Parse(XmlUtils.getGuid(xmlNode2.Name));
									break;
								}
							}
							break;
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return dbRoomInfo;
		}

		public static void UpdateRoomInfoToXML(string fileName, DbRoomInfo info)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "房间信息")
					{
						IEnumerator enumerator2 = xmlNode.ChildNodes.GetEnumerator();
						{
							while (enumerator2.MoveNext())
							{
								object obj2 = enumerator2.Current;
								XmlNode xmlNode2 = (XmlNode)obj2;
								if (xmlNode2.Name == "Id-" + info.Guid.ToString())
								{
									xmlNode2.Attributes["Name"].Value = info.Name;
									xmlNode2.Attributes["Height"].Value = info.Height.ToString();
									xmlNode2.Attributes["Number"].Value = info.Number;
									xmlNode2.Attributes["Function"].Value = info.Function;
									xmlNode2.Attributes["Material"].Value = info.Material;
									xmlNode2.Attributes["RoomId"].Value = info.RoomId.ToString();
									break;
								}
							}
							break;
						}
					}
				}
				xmlDocument.Save(fileName);
			}
			catch
			{
			}
		}

		public static void CreateConfigFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
			xmlDocument.AppendChild(newChild);
			XmlElement xmlElement = xmlDocument.CreateElement("", "连接配置", "");
			xmlDocument.AppendChild(xmlElement);
			XmlElement xmlElement2 = xmlDocument.CreateElement("Server");
			XmlElement xmlElement3 = xmlDocument.CreateElement("Port");
			XmlElement xmlElement4 = xmlDocument.CreateElement("DbName");
			XmlElement xmlElement5 = xmlDocument.CreateElement("Uid");
			XmlElement xmlElement6 = xmlDocument.CreateElement("Pwd");
			XmlElement xmlElement7 = xmlDocument.CreateElement("AutoUser");
			XmlElement xmlElement8 = xmlDocument.CreateElement("AutoPwd");
			xmlElement2.SetAttribute("value", "");
			xmlElement3.SetAttribute("value", "3336");
			xmlElement4.SetAttribute("value", "HyBimService");
			xmlElement5.SetAttribute("value", "sa");
			xmlElement6.SetAttribute("value", "");
			xmlElement7.SetAttribute("value", "");
			xmlElement8.SetAttribute("value", "");
			XmlNode newChild2 = xmlElement.AppendChild(xmlElement2);
			XmlNode newChild3 = xmlElement.AppendChild(xmlElement3);
			XmlNode newChild4 = xmlElement.AppendChild(xmlElement4);
			XmlNode newChild5 = xmlElement.AppendChild(xmlElement5);
			XmlNode newChild6 = xmlElement.AppendChild(xmlElement6);
			XmlNode newChild7 = xmlElement.AppendChild(xmlElement7);
			XmlNode newChild8 = xmlElement.AppendChild(xmlElement8);
			xmlElement.AppendChild(newChild2);
			xmlElement.AppendChild(newChild3);
			xmlElement.AppendChild(newChild4);
			xmlElement.AppendChild(newChild5);
			xmlElement.AppendChild(newChild6);
			xmlElement.AppendChild(newChild7);
			xmlElement.AppendChild(newChild8);
			xmlDocument.Save(fileName);
		}

		public static void WriteConfigFile(string fileName, string noodName, string attributeName, string value)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("连接配置"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == noodName)
					{
						xmlNode.Attributes[attributeName].Value = value;
						break;
					}
				}
				xmlDocument.Save(fileName);
			}
			catch
			{
			}
		}
	}
}
